﻿using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Runtime.ConstrainedExecution;

using HANDLE = System.IntPtr;
using DWORD = System.UInt32;

namespace ActiveDirectoryUtilities.CommonLibrary.Unsafe
{
	static public class Kernel32
	{
		[DllImport("Kernel32", SetLastError = true)]
		private static extern HANDLE GetStdHandle(DWORD nStdHandle);

		[DllImport("Kernel32", SetLastError = true)]
		private static extern bool PeekConsoleInput(HANDLE hConsoleInput, IntPtr lpBuffer, DWORD nLength, out DWORD lpNumberOfEventsRead);

		private const DWORD STD_INPUT_HANDLE = unchecked((DWORD)(-10));

		public static bool stdinRedirected
		{
			get
			{
				HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
				DWORD numOfEventsRead;
				return !PeekConsoleInput(stdinHandle, IntPtr.Zero, 0, out numOfEventsRead);
			}
		}
	}
}
